package com.exam.controller;

import com.exam.dto.LoginDto;
import com.exam.dto.RegisterDto;

import com.exam.service.Impl.UserRoleServiceImpl;
import com.exam.service.Impl.UserServiceImpl;
import com.exam.utils.JwtUtils;
import com.exam.vo.CommonResult;
import com.exam.vo.TokenVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * @Author 小聋包
 * @Date 2024/4/16 19:13
 * @Version 1.0
 */
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/common")
@Api(tags = "(学生,教师,管理员)通用相关接口")
public class CommonController {
    private final UserServiceImpl userService;

    private final UserRoleServiceImpl userRoleService;

    /**
     * 注册
     * @param registerDto
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("用户注册接口")
    @ApiImplicitParams({
            @ApiImplicitParam (name="user",value = "系统用户实体",required=true,dataType="user",paramType="body")})
    public CommonResult<String> Register(@RequestBody @Valid RegisterDto registerDto){
        return CommonResult.<String>builder()
                .data(userService.register(registerDto))
                .build();
    }

    @GetMapping("/check/{username}")
    @ApiOperation("用户名合法查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "系统用户唯一用户名", required = true, dataType = "string", paramType = "path")
    })
    public CommonResult<Boolean> checkUsername(@PathVariable(value = "username") String username) {
        return CommonResult.<Boolean>builder()
                .data(userService.checkUsername(username))
                .build();
    }
    /**
     * 用户登录
     * @param loginDto
     * @return
     */
    @SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
    @ApiOperation("用户登录接口")
    @PostMapping("/login")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "系统用户唯一用户名", required = true, dataType = "string", paramType = "body"),
            @ApiImplicitParam(name = "password", value = "系统用户密码", required = true, dataType = "string", paramType = "body")
    })
    public CommonResult<String> Login(@RequestBody @Valid LoginDto loginDto){
        return CommonResult.<String>builder()
                .data(userService.login(loginDto))
                .build();
    }


    @ApiOperation(value = "获取不同用户的权限菜单")
    @GetMapping("/getMenu")
    public CommonResult<String> getMenu(HttpServletRequest request){
        return CommonResult.<String>builder()
                .data(userRoleService.getMenuInfo(JwtUtils.getUserInfoByToken(request).getRoleId()))
                .build();
    }
    @ApiOperation("验证用户token接口")
    @GetMapping("/checkToken")
    public CommonResult<TokenVo> checkToken(HttpServletRequest request){
        return CommonResult.<TokenVo>builder()
                .data(JwtUtils.getUserInfoByToken(request))
                .build();
    }
}
